package ltd.dujiabao.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.commonutils.JwtUtils;
import ltd.dujiabao.commonutils.MD5;
import ltd.dujiabao.commonutils.ResultCode;
import ltd.dujiabao.servicebase.exception.MyException;
import ltd.dujiabao.ucenterservice.entity.UcenterMember;
import ltd.dujiabao.ucenterservice.entity.vo.LoginVo;
import ltd.dujiabao.ucenterservice.entity.vo.RegisterVo;
import ltd.dujiabao.ucenterservice.mapper.UcenterMemberMapper;
import ltd.dujiabao.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author dujiabao
 * @since 2021-04-24
 */
@Service
@Slf4j
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

  @Autowired
  private RedisTemplate<String, String> redisTemplate;

  @Override
  public String login(LoginVo loginVo) {
    if (loginVo == null || StringUtils.isEmpty(loginVo.getMobile()) || StringUtils.isEmpty(loginVo.getPassword())) {
      log.error("用户名或密码为空");
      throw new MyException(ResultCode.FAILURE, "用户名或密码为空");
    }

    String mobile = loginVo.getMobile();
    String password = MD5.encrypt(loginVo.getPassword()); // 获得加密之后的密码

    QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
    wrapper.select("id", "nickname");
    wrapper.eq("mobile", mobile);
    wrapper.eq("password", password);
    wrapper.eq("is_disabled", false);

    UcenterMember member = getOne(wrapper);
    if (member == null) {
      log.error("用户名或密码为错误");
      throw new MyException(ResultCode.FAILURE, "用户名或密码为错误");
    }
    log.info(member.getId() + " : " + member.getNickname() + "登录成功");
    return JwtUtils.getJwtToken(member.getId(), member.getNickname());
  }

  @Override
  public boolean register(RegisterVo registerVo) {
    if (registerVo == null) {
      log.error("RegisterVo为null");
      throw new MyException(ResultCode.FAILURE, "用户注册失败");
    }
    String mobile = registerVo.getMobile();
    String password = registerVo.getPassword();
    String nickname = registerVo.getNickname();
    String code = registerVo.getCode();

    if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code)) {
      log.error("至少有一个内容为null : " + registerVo.toString());
      throw new MyException(ResultCode.FAILURE, "用户注册失败");
    }

    if (!code.equals(redisTemplate.opsForValue().get(mobile))) {
      log.error("验证码错误/不存在" + redisTemplate.opsForValue().get(mobile));
      throw new MyException(ResultCode.FAILURE, "验证码错误/不存在，用户注册失败");
    }

    QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
    wrapper.eq("mobile", mobile);
    if (count(wrapper) > 0) {
      log.error("手机号已存在");
      throw new MyException(ResultCode.FAILURE, "手机号已存在，用户注册失败");
    }

    UcenterMember member = new UcenterMember();
    BeanUtils.copyProperties(registerVo, member);
    member.setPassword(MD5.encrypt(password)); // 加密密码再放进去

    return save(member);
  }
}
